function login() {
	$("#loginMeg").html('');
	$("#login_div").show();
	$("#fade_div").show();
	initVC($("#loginFailures").val());
	
	var high = $(document).height();
	$("#fade_div").css("height", high + "px");
	
	$("#login_div").focus();
}

function initVC(loginFailures) {
	$("#verifyCode").val('');
	if(loginFailures< 3) {
		$("#vcode").hide();
		$("#vcode2").show();
		
		$("#verifyCode").formValidator({
			validatorGroup:"1"
		});
	} else {
		$("#vcode").show();
		$("#vcode2").hide();
		$("#login_code").attr("src", "/verifyCode" + '?' + new Date().getTime());
		
		$("#verifyCode").formValidator({
			validatorGroup:"1",
			onShow: "请输入验证码",
			onFocus: "4位字母或数字的组合",
			onCorrect: "验证码格式正确"
		}).inputValidator({
			min: 4,
			max: 4,
			empty: {
				leftEmpty: false,
				rightEmpty: false,
				emptyError: "请输入验证码"
			},
			onError: "验证码为4位字母或数字"
		}).regexValidator({
			regExp: "letterAndNO",
			dataType: "enum",
			onError: "只能输入字母或数字"
		});
	}
}

$(document).keyup(function(e){
    var key = e.which;
    if(key == 27){
    	closeLogin();
    }
});

$(document).ready(function() {
	$("#fade_div").click(function() {
		closeLogin();
	});
	
	$("#nav1, #nav2, #nav3, #nav4, #nav5, #nav6").click(function() {
		clickNav(this.id);
	});
	
	var navId = $("#navId").val();
	$("#" + navId).attr("class", "selectMenu");
	
	$("#loginBut").click(function() {
		return $.formValidator.pageIsValid("1");
	});
	
	$.formValidator.initConfig({
		validatorGroup:"1",
		onSuccess: function() {
			var loginName = $("#loginName").val();
			var passWord = $("#passWord").val();
			var loginFailures = $("#loginFailures").val();
			var verifyCode = $("#verifyCode").val();
			
			var jsonStr = {"loginName":loginName, "passWord":passWord, "vcode":verifyCode};
			jQuery.ajax({
				async: false,
				type:"post", 
				dataType: "json",
				contentType:"application/json; charset=utf-8",
				url:"/login",
				data:JSON.stringify(jsonStr),
				success: function(data, textStatus) {
					var loginFailures = data.loginFailures;
					$("#loginFailures").val(loginFailures);
					initVC(loginFailures);
					var code = data.code;
					switch (code) {
				    case 10001: 
				    	$("#loginMeg").html('用户名及密码不能为空！');
				    	break;
				    case 10002: 
				    	$("#loginMeg").html('验证码不能为空！');
				    	break;
				    case 10003: 
				    	$("#loginMeg").html('验证码错误， 请重新输入！');
				    	break;
				    case 20000: 
				    	$("#loginMeg").html('用户名或密码错误！');
				    	break;
				    case 10004: 
				    	$("#loginMeg").html('用户名未激活， 请点击<a href="#">此处激活</a>账号！');
				    	break;
				    case 10005: 
				    	$("#loginMeg").html('用户被冻结，请联系囧神！');
				    	break;
				    case 30000: 
				    	$("#loginMeg").html('系统异常，请联系囧神！');
				    	break;
				    case 10000: 
				    	$("#loginFailures").val(0);
				    	closeLogin();
				    	break;
					}
		      	},
				error : function (XMLHttpRequest, textStatus, errorThrown) {
				    alert("小囧囧，出错了哦， 请联系管理员！！！");
				}
			});
		}
	});
	
	$("#loginName").formValidator({
		validatorGroup:"1",
		onShow: "请输入用户名",
		onFocus: "格式：jsbl@jiong.com",
		onCorrect: "用户名格式正确"
	}).regexValidator({
		regExp: "email",
		dataType: "enum",
		onError: "邮箱格式不正确"
	});
	
	$("#passWord").formValidator({
		validatorGroup:"1",
		onShow: "请输入密码",
		onFocus: "6-18位字母、数字、符号",
		onCorrect: "密码格式正确"
	}).inputValidator({
		min: 6,
		max: 18,
		empty: {
			leftEmpty: false,
			rightEmpty: false,
			emptyError: "请输入密码"
		},
		onError: "密码为6-18位"
	});
	
});

function clickNav(navId) {
	var jsonStr = {"navId": navId};
	jQuery.ajax({
		async: false,
		type: "post",
		dataType: "json",
		contentType: "application/json; charset=utf-8",
		url: "/setNavClass",
		data: JSON.stringify(jsonStr),
		success: function(data, status) {

		},
		error: function(error) {

		}
	});
}

function closeLogin() {
	$("#login_div").hide();
	$("#fade_div").hide();
}

function addFavorite(sURL, sTitle) {
	var sURL = location.href;
	var sTitle = document.title;
	var ctrl = (navigator.userAgent.toLowerCase()).indexOf('mac') != -1 ? 'Command/Cmd': 'CTRL';

	if (document.all) {
		window.external.addFavorite(sURL, sTitle)
	} else if (window.sidebar) {
		window.sidebar.addPanel(sTitle, sURL, "")
	} else {　　　　 // 添加收藏的快捷键
		alert('添加失败\n您可以尝试通过快捷键' + ctrl + ' + D 加入收藏夹！')
	}
}